package manager.servlet;


import com.alibaba.fastjson.JSONObject;
import manager.model.Manager;
import manager.service.MamagerService;
import manager.service.ManagerServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(value = "*.manager")
public class ManagerServlet extends HttpServlet {

    MamagerService mamagerService = new ManagerServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        switch(servletPath) {
            case "/login.manager":
                login(req,resp);
                break;
            case "/queryUserName.manager":
                queryUserName(req,resp);
                break;
            case "/findNumber.manager":
                findNumber(req, resp);
                break;
            case "/logout.manager":
                logout(req,resp);
                break;
            default:
                break;
        }
    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Cookie[] cookies = req.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (int i = 0; i < cookies.length; i++) {
                if ("username".equals(cookies[i].getName())) {
                    cookies[i].setMaxAge(0);
                    resp.addCookie(cookies[i]);
                }
                if ("password".equals((cookies[i].getName()))) {
                    cookies[i].setMaxAge(0);
                    resp.addCookie(cookies[i]);
                }
            }
            req.getSession().removeAttribute("manager");
            resp.sendRedirect(req.getContextPath() + "/login.html");
        }
    }

    protected void findNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        long newsNum = mamagerService.findNewsNumber();
        long usersNum = mamagerService.findUsersNumber();
        long pinglunNum = mamagerService.findPinglunNumber();
        JSONObject json = new JSONObject();
        json.put("newsNum",newsNum);
        json.put("usersNum",usersNum);
        json.put("pnglunNum",pinglunNum);

        String jsonString = json.toString();
        resp.getWriter().print(jsonString);

    }

    protected void findUsersNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        resp.getWriter().print(mamagerService.findUsersNumber());
    }

    protected void findPinglunNumber(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        resp.getWriter().print(mamagerService.findPinglunNumber());
    }

    protected void queryUserName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object managerObj = session.getAttribute("manager");
        if (managerObj != null) {
            Manager manager = (Manager) managerObj;
            String username = manager.getUsername();
            resp.getWriter().print(username);
        }



    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (password != null && !"".equals(password))
            password = DigestUtils.md5Hex(password);
        if (username ==null || "".equals(username)){
            Cookie[] cookies = req.getCookies();
            if (cookies != null && cookies.length > 0){
                for (int i = 0; i < cookies.length; i++) {
                    if ("username".equals(cookies[i].getName()))
                        username = cookies[i].getValue();
                    if ("password".equals(cookies[i].getName()))
                        password = cookies[i].getValue();
                }
            }
        }else {
            Cookie cookieUsername = new Cookie("username",username);
            cookieUsername.setMaxAge(30);
            Cookie cookiePassword = new Cookie("password",password);
            cookiePassword.setMaxAge(30);
            resp.addCookie(cookieUsername);
            resp.addCookie(cookiePassword);
        }
        if (username == null || "".equals(username)){
            resp.sendRedirect(req.getContextPath() + "/login.html");
            return;
        }
        boolean res = mamagerService.isLogin(username,password, req.getSession());
        if (res)
            resp.sendRedirect(req.getContextPath() + "/main111.html");
        else
            resp.sendRedirect(req.getContextPath() + "/login.html");
    }
}
